package com.example.common.threadCurrent;

public class TestThreadLocal {

    static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    static void printT(String str) {
        System.out.println(str +":"+threadLocal.get());
        threadLocal.remove();
    }

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
           threadLocal.set("localVar1");
           printT("t1");
            System.out.println("after remove:" + threadLocal.get());
        });
        Thread t2 = new Thread(() -> {
           threadLocal.set("localVar2");
           printT("t2");
            System.out.println("after remove:" + threadLocal.get());
        });
        t1.start();
        t2.start();
    }
}

class RunnableThreadLocal implements Runnable{

    int counter = 0;
    ThreadLocal<String> threadLocal = new ThreadLocal<>();

    @Override
    public void run() {
        threadLocal.set(Thread.currentThread().getName() + String.valueOf(counter++));
    }
}